﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Editor
{
    public class Grid
    {
        private EditorCursor _cursor;
        private Graphics _drawingSurface;
        private Graphics _backgroundGraphics;
        private RectangleF _drawingScreenBounds;
        private Bitmap _mapImage;
        private Board _gameBoard;
        private Size _cellSize;
        private int _rows;
        private int _columns;
        private int _gridX;
        private int _gridY;
        private Point _tileScroll = new Point(0, 0);
        private TileList _tileList;
        private int _selectedTileX
        {
            get { return _tileScroll.X + _cursor.OldX; }
        }
        private int _selectedTileY
        {
            get { return _tileScroll.Y + _cursor.OldY; }
        }
        

        public Grid(ref Graphics graphicsSurface,ref Board gameboard, ref TileList tileList, Size cellSize, int rows, int columns)
        {
            _drawingSurface = graphicsSurface;
            _drawingScreenBounds = _drawingSurface.VisibleClipBounds;
            _cellSize = cellSize;
            _rows = rows;
            _columns = columns;
            _tileList = tileList;
            InitBackground();
            Init();
            _gameBoard = gameboard;
            _cursor.AssignNewBitmap(_gameBoard._board);
        }

        private void Init()
        {
            _cursor = new EditorCursor(ref _drawingSurface, ref _mapImage);
            _cursor.HEIGHT = _cellSize.Height;
            _cursor.WIDTH = _cellSize.Width;
        }

        private void InitBackground()
        {
            _mapImage = new Bitmap(32 * _columns, 32 * _rows);
            _backgroundGraphics = Graphics.FromImage(_mapImage);
            _backgroundGraphics.Clear(Color.Black);
        }

        public void MapClicked(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            UpdateMousePosition(e.Location);
            _tileList.AssignBoardTile(_selectedTileY, _selectedTileX);
            _cursor.Draw();
        }

        public void UpdateMousePosition(Point mouseLocation)
        {
            _gridX = (int)(mouseLocation.X / _cellSize.Width);
            _gridY = (int)(mouseLocation.Y / _cellSize.Height);
            UpdateCursor();
        }

        private void UpdateCursor()
        {
            _cursor.UpdatePosition(_gridX, _gridY);
        }
        
        public void ScrollHeight(object sender, System.Windows.Forms.ScrollEventArgs e)
        {
            _tileScroll.Y = e.NewValue;
            ScrollImage();
        }

        public void ScrollWidth(object sender, System.Windows.Forms.ScrollEventArgs e)
        {
            _tileScroll.X = e.NewValue;
            ScrollImage();
        }

        public void ScrollImage()
        {
            Point drawPoint = new Point((_tileScroll.X) * 32, (_tileScroll.Y) * 32);
            _cursor._backgroundImageLocation = drawPoint;
            _cursor.Draw();
        }

        public void AssignTile(Point Location)
        {
            UpdateMousePosition(Location);
            _tileList.AssignBoardTile(_selectedTileY, _selectedTileX);
            _cursor.Draw();
        }
    }
}
